import { ref, onMounted } from "vue";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
import { postAction } from "src/utils/requestAction";
import { DictOptions } from "src/utils/dict";
import useCommon from "./useCommon";

export default function useTableData(url, defaultSort = false) {
  const { t } = useI18n();
  const $q = useQuasar();
  const dictOptions = ref({});
  onMounted(async () => {
    const dictOptionsOri = await DictOptions();
    //console.log(dictOptionsOri);

    // 用字典里的sort字段排个序
    for (let dict in dictOptionsOri) {
      dictOptions.value[dict] = dictOptionsOri[dict].sort(
        (a, b) => a.sort - b.sort
      );
    }
  });
  const loading = ref(false);
  const tableData = ref([]);
  const recordDetailDialog = ref(null);
  const pagination = ref({
    sortBy: "sort",
    descending: defaultSort,
    page: 1,
    rowsPerPage: 20,
  });

  //参数
  const queryParams = ref({});

  //分页选项
  const pageOptions = ref([10, 20, 35, 50, 100, 0]);

  const showAddForm = () => {
    console.log("showAddForm");
    recordDetailDialog.value.formType = "add";
    console.log(recordDetailDialog.value);
    recordDetailDialog.value.show();
  };
  const showEditForm = (row) => {
    console.log("showEditForm");
    recordDetailDialog.value.formType = "edit";
    console.log(recordDetailDialog.value);
    recordDetailDialog.value.show(row);
  };

  const onRequest = async (props) => {
    if (url === undefined || !url.list) {
      $q.notify({
        type: "negative",
        message: t("UrlNotConfig"),
      });
      return;
    }

    tableData.value = [];
    loading.value = true;
    const params = {};
    params.sort_by = props.pagination.sortBy;
    params.desc = props.pagination.descending;
    params.page = props.pagination.page;
    params.page_size = props.pagination.rowsPerPage;
    const allParams = Object.assign({}, params, queryParams.value);
    await postAction(url.list, allParams)
      .then((res) => {
        if (res.code === 1) {
          console.log(pagination.value);
          // 最终要把分页给同步掉
          pagination.value = props.pagination;
          // 并且加入总数字段
          pagination.value.rowsNumber = res.data.total;
          tableData.value = res.data.records || [];
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const getTableData = () =>
    onRequest({ pagination: pagination.value, queryParams: queryParams.value });
  const getAllData = () =>
    onRequest({
      pagination: { page: 0, page_size: 0 },
      queryParams: queryParams.value,
    });
  const handleSearch = () => {
    getTableData();
  };
  const resetSearch = () => {
    queryParams.value = {};
    getTableData();
  };
  const handleFinish = () => {
    getTableData();
  };
  const handleDelete = (row, msg) => {
    if (!url || !url.delete) {
      $q.notify({
        type: "negative",
        message: "请先配置url",
      });
      return;
    }
    $q.dialog({
      title: t("Confirm"),
      message: msg ? msg : t("Confirm") + t("Delete") + "?",
      cancel: true,
      persistent: true,
    }).onOk(async () => {
      const res = await postAction(url.delete, {
        id: row.id,
      });
      if (res.code === 1) {
        $q.notify({
          type: "positive",
          message: res.message,
        });
      }
      getTableData();
    });
  };

  const { DcDictCell } = useCommon();
  return {
    DcDictCell,
    dictOptions,
    pagination,
    loading,
    queryParams,
    pageOptions,
    tableData,
    recordDetailDialog,
    onRequest,
    getTableData,
    getAllData,
    handleSearch,
    resetSearch,
    handleFinish,
    showAddForm,
    showEditForm,
    handleDelete,
  };
}
